package com.itheima.web.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.Role;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@WebServlet("/system/role")
public class RoleServlet extends BaseServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)) {
            findAll(req, resp);
        } else if ("toAdd".equals(operation)) {
            toAdd(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            update(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        } else if ("author".equals(operation)) {
            author(req, resp);
        }else if ("updateRoleModule".equals(operation)) {
            updateRoleModule(req, resp);
        }
    }


    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 10;
        if (StringUtils.isNoneBlank(req.getParameter("page"))) {
            page = Integer.parseInt(req.getParameter("page"));
        }
        if (StringUtils.isNoneBlank(req.getParameter("size"))) {
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo all = roleService.findAll(page, size);
        req.setAttribute("page", all);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/list.jsp").forward(req, resp);
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> all = roleService.findAll();
        req.setAttribute("roleList", all);

        req.getRequestDispatcher("/WEB-INF/pages/system/role/add.jsp").forward(req, resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Role role = BeanUtil.fillBean(req, Role.class, "yyyy-MM-dd");

        roleService.save(role);
        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Role role = roleService.findById(id);
        req.setAttribute("role", role);
        List<Role> all = roleService.findAll();
        req.setAttribute("roleList", all);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/update.jsp").forward(req, resp);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Role role = BeanUtil.fillBean(req, Role.class, "yyyy-MM-dd");
        roleService.update(role);
        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] id = req.getParameter("id").split(",");
        roleService.delete(id);
        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    private void author(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("id");
        Role role = roleService.findById(roleId);
        req.setAttribute("role", role);
        List<Map> map = moduleService.findAuthorDataByRId(roleId);
        System.out.println("===============================================");
        System.out.println(map);
        System.out.println("===============================================");
        ObjectMapper obm = new ObjectMapper();
        String json = obm.writeValueAsString(map);
        req.setAttribute("roleModuleJson",json);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(req, resp);
    }
    private void updateRoleModule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
//        Role role = roleService.findById(roleId);
        String[] moduleIds = req.getParameter("moduleIds").split(",");
        roleService.updateRoleModule(roleId,moduleIds);

        resp.sendRedirect(req.getContextPath() + "/system/role?operation=list");
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
